﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace jnxceq
{
    public partial class Test : Form
    {
        public Test()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string str = this.textBox1.Text;

            
        }
       
        /// <summary>
        /// 
        /// </summary>
        /// <param name="RcvBytes"></param>
        /// <returns>0、无效包，1、心跳包，2、数据包</returns>
        public int CheckData(string RcvBytes)
        {
            int result = 0;

            if (!string.IsNullOrEmpty(RcvBytes))
            {
                int lenth = 0;//数据长度

                if (RcvBytes.Substring(0, 4) == "7EFA")//开头
                {
                    string len1 = RcvBytes.Substring(4, 4);//长度

                    lenth = Convert.ToInt32(len1, 16);

                    if (RcvBytes.Substring(lenth*2 + 8,2) == "7E")//结尾
                    {
                        if (lenth == 3)//3为心跳包
                        {
                            string heart = RcvBytes.Substring(8, lenth * 2);

                            if (heart == "726571")//心跳（ASCII码为req）
                            {
                                result = 1;
                            }
                            else
                            {
                                result = 2;
                            }

                        }
                    }
                    

                   
                }

              
            }

            return result;

        }

        private void Test_Load(object sender, EventArgs e)
        {

        }
           
           
    }
}
